//
//  ServiceBuyersViewController.m
//  GLGW_Shoping
//
//  Created by yang on 2018/12/10.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "ServiceBuyersViewController.h"
#import "ServiceBuyerViewModel.h"
#import "AddNewBuyersViewController.h"
#import "ServiceBuyersModel.h"

@interface ServiceBuyersViewController ()<ServiceBuyersDelegate>{
    ServiceBuyersModel * serviceBuyersModel;
    NSMutableDictionary * requestDic;
}
@property(nonatomic,strong)ServiceBuyerViewModel * serviceViewModel;
@end

@implementation ServiceBuyersViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.serviceViewModel = [[ServiceBuyerViewModel alloc] initWithViewToViewModel:self.tableView theSuperView:self.view];
    self.serviceViewModel.delegate = self;
    requestDic = [NSMutableDictionary dictionaryWithDictionary:@{@"status":@(self.serviceType),@"userId":koString(self.userId),@"pageSize":@10}];
    [self headerRereshing];
}

#pragma mark -- ServiceBuyersDelegate
- (void)addNewBuyersAction:(ServiceBuyersModel_Infos *)ser_infors
{
    AddNewBuyersViewController * addNewVc = [AddNewBuyersViewController new];
    addNewVc.s_inforModel = ser_infors;
    addNewVc.Block_EditOrAddAction = ^(AddOrEditType t_edit) {
        [self headerRereshing];
    };
    [self.navigationController pushViewController:addNewVc animated:YES];
}

#pragma mark -- set
- (void)setServiceType:(MyServiceType)serviceType
{
    _serviceType = serviceType;
}

#pragma mark -- 数据请求
- (void)headerRereshing
{
    self.page = 1;
    [self loadData];
}

- (void)footerRereshing
{
    if (self.page < serviceBuyersModel.data.pageCount) {
        self.page ++;
        [self loadData];
    }else{
        [GLGWProgressHUD showNotice:@"没有更多服务的买家了！" view:nil];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
    }
}

- (void)loadData
{
    [requestDic setObject:@(self.page) forKey:@"pageNo"];
    @weakify(self)
    [[self.serviceViewModel.service_RecommendCommand execute:requestDic] subscribeNext:^(id  _Nullable x) {
        @strongify(self)
        self->serviceBuyersModel = (ServiceBuyersModel *)x;
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
    } error:^(NSError * _Nullable error) {
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
    }];
}

@end
